﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;

namespace remes.BLoc.UI
{
   /// <summary>
   /// Interaction logic for SelectCultureInfoDialog.xaml
   /// </summary>
   public partial class SelectCultureInfoDialog : Window
   {
      public SelectCultureInfoDialog()
      {
         InitializeComponent();
         ExclusiveLanguages = null;
      }

      public IEnumerable<string> ExclusiveLanguages { get; set; }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
         var languages = new List<string>();
         foreach (var culture in cultures)
         {
            languages.Add(culture.Name);
         }
         languages.Sort();

         foreach (var lang in languages)
         {
            if (!String.IsNullOrWhiteSpace(lang) && !IsExclusiveLanguage(lang))
            {
               CultureInfoList.Items.Add(lang);
            }
         }
      }

      private bool IsExclusiveLanguage(string lang)
      {
         if (ExclusiveLanguages != null)
         {
            foreach (var l in ExclusiveLanguages)
            {
               if (String.Compare(lang, l, StringComparison.OrdinalIgnoreCase) == 0)
               {
                  return true;
               }
            }
         }
         return false;
      }

      private void OkButton_Click(object sender, RoutedEventArgs e)
      {
         if (CultureInfoList.SelectedItem != null)
         {
            DialogResult = true;
            Close();
         }
      }

      public string SelectedLanguage
      {
         get { return CultureInfoList.SelectedItem != null ? (string)CultureInfoList.SelectedItem : null; }
      }
   }
}
